function addFunction(arg1: number, arg2: number): number {
    return arg1 + arg2
}
const addFunction2 = (arg1: number, arg2: number): number => arg1 + arg2

let addFunc: (arg1: number, arg2: number) => number // 定义函数
addFunc = (arg1: number, arg2: number) => arg1 + arg2 // 函数的实现
console.log(addFunc(1, 2));

// 定义函数类型|函数别名
type isString = string

interface IAdd {
    // tslint:disable-next-line: callable-types
    (arg1: number, arg2: number): number
}
// 通过函数类型定义函数
let addIAdd: IAdd
addIAdd = (arg1: number, arg2: number) => arg1 + arg2


type TAdd = (arg1: number, arg2?: number) => number // 定义函数类型(可选参数)
let addTAdd: TAdd
addTAdd = (arg1: number, arg2?: number) => arg1 + (arg2 ? arg2 : 0)
console.log(addTAdd(1, 2));

type DAdd = (arg1: number, arg2?: number) => number // 定义函数类型(定义时不允许设置默认值）
let addDAdd: DAdd
addDAdd = (arg1: number, arg2: number = 3) => arg1 + arg2
console.log(addDAdd(2));

// 定义未知长度的参数
type ArgsAdd = (arg1: number, ...args: number[]) => number
let addArgsAdd: ArgsAdd;
addArgsAdd = (arg1, arg2, arg3) => arg1 + arg2 + arg3
console.log(addArgsAdd(5, 6, 7))

// 函数的重载(函数定义的时候可以重载)
function handData(x: string): string[]
function handData(x: number): number[]
// 函数体本身无法重载
function handData(x: string | number): string[] | number[] {
    if (typeof x === "string") {
        return x.split("")
    } else {
        return x.toString().split("").map(i => Number(i))
    }
}
console.log(handData("123"), handData(123));


